package com.xlhj.boot.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: caijliu
 * @Date: 03/09/2021 9:13
 * @Description TODO
 * @Vsersion: 0.0.1
 */
@Configuration
public class RabbitTopicConfig {

    private static final String TOPIC_EXCHANGE_NAME = "topic-exchange";
    private static final String TOPIC_QUEUE_NAME_A = "topic-queue-A";
    private static final String TOPIC_QUEUE_NAME_B = "topic-queue-B";
    private static final String ROUTING_KEY_A = "topic.*.*";
    private static final String ROUTING_KEY_B = "*.*.rabbit";

    /**
     * 声明Exchange
     * @return
     */
    @Bean(name = "topicExchange")
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE_NAME);
    }

    /**
     * 声明队列
     * @return
     */
    @Bean(name = "topicQueueA")
    public Queue topicQueueA() {
        return QueueBuilder.durable(TOPIC_QUEUE_NAME_A).build();
    }

    /**
     * 声明队列
     * @return
     */
    @Bean(name = "topicQueueB")
    public Queue topicQueueB() {
        return QueueBuilder.durable(TOPIC_QUEUE_NAME_B).build();
    }

    /**
     * 声明确认队列绑定关系
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding topicQueueBindingA(@Qualifier("topicQueueA") Queue queue, @Qualifier("topicExchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_A);
    }

    /**
     * 声明确认队列绑定关系
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding topicQueueBindingB(@Qualifier("topicQueueB") Queue queue, @Qualifier("topicExchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_B);
    }
}
